package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.TableTypeDAO;
import com.junzhiit.nengji.model.TableType;
import com.junzhiit.nengji.model.enums.BookingStatus;
import com.junzhiit.nengji.model.enums.Is;

@Repository
public class TableTypeDAOImpl extends AbstractBaseDAO<TableType> implements TableTypeDAO{
	@Override
	public List<TableType> findPageAll(TableType tableType, int pageNumber, int maxPageRow) {
		
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from table_type");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(tableType.getShopId())){
			sql.append(" and shop_id = ?");
			param.setString(tableType.getShopId());
		}

		return super.queryForPagingList(sql.toString(), TableType.class, param, pageNumber,maxPageRow);
	}
	
	@Override
	public int getTotalCount(TableType tableType) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(*) from table_type");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(tableType.getShopId())){
			sql.append(" and shop_id = ?");
			param.setString(tableType.getShopId());
		}
		
		if(StringUtils.isNotBlank(tableType.getName())){
			sql.append(" and name = ?");
			param.setString(tableType.getName());
		}
		
		return super.queryForInt(sql.toString(), param);
	}
	
	
	@Override
	public List<TableType> getAll(TableType tableType) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from table_type ");
		sql.append(" where is_delete=? ");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(tableType.getShopId())){
			sql.append(" and shop_id = ? ");
			param.setString(tableType.getShopId());
		}
		
		return super.queryForList(sql.toString(), TableType.class, param);
	}
	

	@Override
	public List<TableType> findAll(String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from table_type ");
		sql.append(" where is_delete=? and shop_id = ? ");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(shopId);
		
		sql.append(" order by min_number desc ");
		
		return super.queryForList(sql.toString(), TableType.class, param);
	}

	@Override
	public int surplusTable(String tableTypeId) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param=new StatementParameter();
		sql.append(" select ( ");
		sql.append(" select count(*) from table_number tn left join table_type tt on tn.table_type_id = tt.id ");
		sql.append(" where tn.table_type_id = ? and tn.is_delete=? and tt.is_delete=? ");
		sql.append(" ) - ( ");
		sql.append(" select count(*) from booking b left join table_type tt on b.table_type_id = tt.id ");
		sql.append(" where b.table_type_id = ? and datediff(b.choose_time,now()) = ? and b.status in(?,?) and b.is_delete=? ) ");
		param.setString(tableTypeId);
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(tableTypeId);
		param.setInt(Is.NO.toIntValue());
		param.setInt(BookingStatus.PAYMENT.toIntValue());
		param.setInt(BookingStatus.CONFIRM.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.queryForInt(sql.toString(), param);
	}
	
	
}
